import { defineStore } from 'pinia'
import dayjs from 'dayjs'
import { getCache, removeCache, setCache } from '@/utils/cache'
import { TOKEN_KEY, USER_INFO_KEY } from '@/enums/cacheEnum'

const defaultTokenModel: ApiModels.base.TokenModel= {
  token: '',
  expire_time: '',
  mobile: '',
  is_new_user: false,
}

interface AuthState {
  tokenModel: ApiModels.base.TokenModel
}

export const useAuthStore = defineStore({
  id: 'auth',
  state: (): AuthState => ({ tokenModel: { ...defaultTokenModel } }),
  getters: {
    getToken: state => state?.tokenModel?.token || '',
    isLogin: (state): boolean => state && !!state.tokenModel.token && dayjs().isBefore(state.tokenModel.expire_time * 1000, 'second'),
  },
  actions: {
    initToken() {
      const data = getCache<ApiModels.base.TokenModel>(TOKEN_KEY) || { ...defaultTokenModel }
      this.tokenModel = { ...data }
    },

    setToken(data: ApiModels.base.TokenModel, expire?: number | null) {
      this.tokenModel = { ...data }
      setCache(TOKEN_KEY, data, expire)
    },

    /**
     * @description 登录
     */
    async login(params): Promise<ApiModels.base.TokenModel | undefined> {
      try {
        // const { error, data } = await accountLogin(params)
        // if (error)
        //   return Promise.reject(error)
        // return Promise.resolve(data)
      }
      catch (err: any) {
        return Promise.reject(err)
      }
    },
    /**
     * @description 登出
     */
    // async loginOut(): Promise<any> {
    //   try {
    //     const res = await logout()
    //     removeCache(TOKEN_KEY)
    //     removeCache(USER_INFO_KEY)
    //     this.tokenModel = {}
    //     return Promise.resolve(res)
    //   }
    //   catch (err: any) {
    //     return Promise.reject(err)
    //   }
    // },
  },
})
